﻿using System;
using System.Linq;
using Sushi.Core;
using Sushi.Sample.Service.Data;

using Exception = System.Exception;

namespace Sushi.Sample.Service.WCFService
{
   /// <summary>
   /// Provides a concrete implementation of the <see cref="ISushiService"/> interface.
   /// </summary>
   public class SushiService : ISushiService
   {
      #region ISushiCounterService Members
      /// <summary>
      /// Retrieves a Counter report response that corresponds with the provided request.
      /// </summary>
      /// <param name="request">The report request.</param>
      /// <returns>A Counter report response.</returns>
      public GetReportResponse GetReport(GetReportRequest request)
      {
         var businessLogic = new SushiComponent(new UsageReportRepository(), new AuthorizationAuthority());

         var response = new GetReportResponse()
                           {
                              ReportResponse = new CounterReportResponse()
                                                  {
                                                     Created = DateTime.Now,
                                                     CreatedSpecified = true,
                                                     CustomerReference = request.ReportRequest.CustomerReference,
                                                     ID = request.ReportRequest.ID,
                                                     ReportDefinition = request.ReportRequest.ReportDefinition,
                                                     Requestor = request.ReportRequest.Requestor,
                                                  }
                           };
         try
         {
            response.ReportResponse.Report = businessLogic.GetSushiReports(request.ReportRequest);
         }
         catch (Exception ex)
         {
            if (ex is SushiServiceException)
					response.ReportResponse.Exception = ex.ToSushiExceptions().ToArray();
            else
            {
            	throw;
            }
         }

         return response;
      }
      #endregion
   }
}